<script setup lang="ts">
import { ref } from 'vue'
import { useMemberStore } from '../../stores/modules/member'
import { useRoleStore } from '../../stores/modules/role'
import { busListStore } from '../../stores/modules/bus'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { postLoginWxMinAPI } from '@/api/account'
import type { userItem } from '@/types/member'
import {baseURL} from "@/utils/http";

const id = ref('')
const roleStore = useRoleStore()
const memberStore = useMemberStore()
const busStore = busListStore()
const status = ref<string>('')
const profile = ref<userItem>({})

// 获取个人信息
const getMemberProfileData = async () => {
  if (!memberStore.profile) {
    const rescode = await wx.login()
    const { code } = rescode
    const loginrRes = await postLoginWxMinAPI(code)
    profile.value = loginrRes.data.appUser
  } else {
    profile.value = memberStore.profile
  }
}

onShow(() => {
  // 判断是否登陆
  if (memberStore.profile) {
    id.value = memberStore.profile.userId
    getMemberProfileData()
    uni.hideLoading()
    if (busStore.busList?.length > 0) {
      status.value = busStore.busList[0].status
    }
    // 判断是否有角色权限
    if (roleStore.roles) {
      if (roleStore.roles.includes('bus_user')) {
        isRole.value = true
        status.value = busStore.busList[0].state
      } else if (roleStore.roles.includes('agent_user')) {
        uni.redirectTo({ url: '/pages/indexAgent/indexAgent' })
      } else if (roleStore.roles.includes('bus_agent')) {
        uni.redirectTo({ url: '/pages/indexBusinessAgent/indexBusinessAgent' })
      } else if (roleStore.roles.includes('delivery_user')) {
        uni.redirectTo({ url: '/pages/carry/carry' })
      } else {
        uni.redirectTo({ url: '/pages/my/my' })
      }
    }
  } else {
    uni.navigateTo({ url: '/pages/login/login' })
  }
})
const isRole = ref(false)

const gotoScan = () => {
  uni.navigateTo({ url: '/pages/activity/activity' })
}
</script>

<template>
  <view class="navs" v-if="isRole">
    <navigator url="/pages/people/people" class="item">
      <image src="@/static/images/business_05.png" mode="aspectFit" />
      <text>人员管理</text>
    </navigator>
    <navigator url="/pages/reservation/reservation" class="item">
      <image src="@/static/images/business_01.png" mode="aspectFit" />
      <text>预约纸码</text>
    </navigator>
    <navigator url="/pages/activity/activity" class="item">
      <image src="@/static/images/business_02.png" mode="aspectFit" />
      <text>发起活动</text>
    </navigator>
    <navigator url="/pages/myCode/myCode" class="item">
      <image src="@/static/images/business_03.png" mode="aspectFit" />
      <text>我发的码</text>
    </navigator>
    <!-- <navigator url="/pages/otherCode/otherCode" class="item">
      <image src="@/static/images/business_04.png" mode="aspectFit" />
      <text>别家的码</text>
    </navigator>
    <navigator url="/pages/delivery/delivery" class="item">
      <image src="@/static/images/business_06.png" mode="aspectFit" />
      <text>配送中心</text>
    </navigator> -->
    <navigator url="/pages/prize/prize" class="item">
      <image src="@/static/images/business_07.png" mode="aspectFit" />
      <text>中奖列表</text>
    </navigator>
    <navigator :url="'/pages/company/company?id=' + id" class="item">
      <image src="@/static/images/business_08.png" mode="aspectFit" />
      <text>资料管理</text>
    </navigator>
    <!-- <navigator url="/pages/wallet/wallet" class="item">
      <image src="@/static/images/business_09.png" mode="aspectFit" />
      <text>钱包</text>
    </navigator> -->
    <!-- <navigator url="/pages/indexAgent/indexAgent" class="item">
      <image src="@/static/images/business_09.png" mode="aspectFit" />
      <text>代理商</text>
    </navigator> -->
  </view>
  <view class="scan" v-if="isRole"
    ><image @tap="gotoScan" class="scan_image" src="@/static/images/cv-sys.png" mode="aspectFill"
  /></view>
  <template v-else>
    <view class="turn company_info" v-if="status == '1'">
      <text>您已经提交了入驻信息，</text>
      <text>{{ status == '1'?'正在进行审核中...':status == '2'?'审核通过':'审核不通过' }}</text>
      <text>请耐心等待</text>
      <navigator :url="'/pages/company/company?id=' + id" class="button phone">
        查看我的入驻信息
      </navigator>
    </view>
    <view class="turn" v-else>
      <text>您还没有入驻，选择想要的入驻类型吧</text>
      <navigator url="/pages/join/join" class="button phone">我要入驻</navigator>
    </view>
  </template>
</template>

<style lang="scss">
.navs {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  align-items: center;
  padding: 40rpx 20rpx;
  .item {
    width: 200rpx;
    height: 200rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border-radius: 10rpx;
    margin-bottom: 40rpx;
    image {
      width: 100rpx;
      height: 100rpx;
    }
    text {
      font-size: 26rpx;
    }
  }
  .item:nth-of-type(1) {
    background-color: #fff5f1;
  }
  .item:nth-of-type(2) {
    background-color: #f2fefe;
  }
  .item:nth-of-type(3) {
    background-color: #fff9ef;
  }
  .item:nth-of-type(4) {
    background-color: #fff5f8;
  }
  .item:nth-of-type(5) {
    background-color: #f5f4ff;
  }
  .item:nth-of-type(6) {
    background-color: #fff5f1;
  }
  .item:nth-of-type(7) {
    background-color: #f4ffe9;
  }
  .item:nth-of-type(8) {
    background-color: #fff9ef;
  }
  .item:nth-of-type(9) {
    background-color: #f8f3ff;
  }
}
.turn {
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding-top: 150px;
  text {
    display: inline-block;
    padding: 0 50px;
  }
  .button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 300rpx;
    height: 80rpx;
    font-size: 28rpx;
    border-radius: 72rpx;
    color: #fff;
    margin: 40rpx 0 80rpx;
    .icon {
      font-size: 40rpx;
      margin-right: 6rpx;
    }
  }

  .phone {
    background-color: #28bb9c;
  }
}
</style>
